//
//  KSAboutViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/19.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

class KSAboutViewController: KSBaseViewController {

    @IBOutlet weak var tableview: UITableView!
    private let dataArray = ["新版本介绍","使用许可协议"]
    private var accountSetModel: KSAccountModel?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "关于吉拍档"
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = KSColor.kColorViewBackground
        self.tableview.register(KSOnlyTitleTableViewCell.self, forCellReuseIdentifier: "KSOnlyTitleTableViewCell")
        self.tableview.register(KSAboutAppHeadTableViewCell.self, forCellReuseIdentifier: "KSAboutAppHeadTableViewCell")
        
        self.request.requestAccountVersion()
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        self.accountSetModel = KSAccountModel.deserialize(from: result)
    }
    
    override func requestFaile(code: Int, message: String, apiType: KSApiType) {
        tableview.isUserInteractionEnabled = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension KSAboutViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var cellHeight: CGFloat = 50
        if  indexPath.row == 0 {
            cellHeight = 188
        }
        return cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableview.dequeueReusableCell(withIdentifier: "KSAboutAppHeadTableViewCell", for: indexPath) as! KSAboutAppHeadTableViewCell
            return cell
        } else {
            let cell = tableview.dequeueReusableCell(withIdentifier: "KSOnlyTitleTableViewCell", for: indexPath) as! KSOnlyTitleTableViewCell
            cell.accessoryType = .disclosureIndicator
            cell.selectionStyle = .none
            cell.titleLabel.text = dataArray[indexPath.row - 1]
            if indexPath.row == 2 {
                cell.lineView.isHidden = true
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row > 0 {
            let accountModel = (self.accountSetModel?.data![indexPath.row - 1])!
            let vc = KSWebViewController()
            vc.url = accountModel.url
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
 
}

